package com.atguigu.dga.assessor.calc;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.dga.assessor.Assessor;
import com.atguigu.dga.governance.bean.AssessParam;
import com.atguigu.dga.governance.bean.GovernanceAssessDetail;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.text.ParseException;
import java.util.Calendar;
import java.util.Date;
import java.util.concurrent.TimeUnit;

@Component("NO_ACCESS")
public class NoAccessAssessor extends Assessor {
    @Override
    public void checkProblem(GovernanceAssessDetail governanceAssessDetail, AssessParam assessParam) throws ParseException {
        //获取指标
        String metricParamsJson = assessParam.getGovernanceMetric().getMetricParamsJson();
        JSONObject jsonObject = JSONObject.parseObject(metricParamsJson);
        String da =jsonObject.getString("days");
        Integer integerda = Integer.valueOf(da);
        //取最后一次登录时间
        Date tableLastAccessTime = assessParam.getTableMetaInfo().getTableLastAccessTime();
        Date lastdate = DateUtils.truncate(tableLastAccessTime, Calendar.DATE);
        //取当前时间
        String assessDate = assessParam.getAssessDate();
        Date date = DateUtils.parseDate(assessDate, "yyyy-MM-dd");
        //计算时间差值
        long abs = Math.abs(date.getTime() - lastdate.getTime());
        //转成天
        long convert = TimeUnit.DAYS.convert(abs, TimeUnit.MILLISECONDS);
        if (convert>integerda){
            governanceAssessDetail.setAssessScore(BigDecimal.ZERO);
            governanceAssessDetail.setAssessProblem("长期未被访问");
            governanceAssessDetail.setAssessComment("超过 " + convert + " 天未被访问");
        }

    }
}
